package org.eclipseuseradmin.internal.ui;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipseuseradmin.ui.IRoleRootDescriptor;

public class RoleRootsRegistry {

	private static final String EXTENSION_POINT = "roleRoots";
	
	private List<IRoleRootDescriptor> roleRootElements;

	public List<IRoleRootDescriptor> getRoleRoots() {

		if (roleRootElements == null) {
			roleRootElements = new ArrayList<IRoleRootDescriptor>();
			parseRegistry();
		}

		return roleRootElements;
	}
	
	protected void parseRegistry() {

		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint point = registry.getExtensionPoint(
				UserAdminUiPlugin.PLUGIN_ID, EXTENSION_POINT);
		if (point == null) {
			return;
		}

		IExtension[] extensions = point.getExtensions();
		for (IExtension extension : extensions) {
			IConfigurationElement[] elements = extension
					.getConfigurationElements();
			for (IConfigurationElement element : elements) {

				roleRootElements.add(getRoleRootElement(element));
			}
		}
	}

	private RoleRootElement getRoleRootElement(IConfigurationElement config) {

		return new RoleRootElement(config);
	}
}
